<template>
    <el-dialog :close-on-click-modal="false"
               :title="!dataForm.id ? '新增' : '修改'"
               :visible.sync="visible"
               width="1080px">
        <el-form ref="dataForm"
                 :model="dataForm"
                 :rules="dataRule"
                 label-width="120px"
                 @keyup.enter.native="dataFormSubmit()">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="产品意向" prop="busiOppoId">
                        <el-select v-model="dataForm.busiOppoId" placeholder="请选择">
                            <el-option
                                v-for="item in sjList"
                                :key="item.id"
                                :label="item.buyIntention"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="跟踪类型" prop="traceKind">
                        <el-select v-model="dataForm.traceKind" placeholder="请选择">
                            <el-option
                                v-for="item in traTypeList"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="当前跟踪人" prop="traceMan">
                        <el-select v-model="dataForm.traceMan" placeholder="请选择">
                            <el-option
                                v-for="item in personList"
                                :key="item.id"
                                :label="item.empName"
                                :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label="跟踪时间" prop="traceTime">
                        <el-date-picker
                            v-model="dataForm.traceTime"
                            placeholder="选择日期时间"
                            type="datetime"
                            value-format="yyyy-MM-dd HH:mm:ss">
                        </el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="联系电话"
                                  prop="relConnectTel">
                        <el-input v-model="dataForm.relConnectTel"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="跟踪描述"
                          prop="traceDesc">
                <el-input v-model="dataForm.traceDesc"></el-input>
            </el-form-item>
            <el-form-item label="客户反馈意见"
                          prop="cusFeedback">
                <el-input v-model="dataForm.cusFeedback"
                          placeholder="产品意向"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer"
              class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
import {Debounce} from '@/utils/debounce'
import {gzTypeList} from '@/utils/data'

export default {
	data () {
		return {
			visible: false,
			roleList: [],
			personList: [],
			sjList: [],
			traTypeList: gzTypeList,
			provinceList: [],
			cityList: [],
			areaList: [],
			dataForm: {
				id: null,
				busiOppoId: '',
				traceKind: '',
				cusName: '',
				traceDesc: '',
				relConnectTel: '',
				cusProvince: '',
				cusCity: '',
				cusAddress: '',
				cusFeedback: '',
				traceMan: '',
				traceTime: '',
				biz1: ''
			},
			dataRule: {
                busiOppoId: [
                    {required: true, message: '请选择产品意向', trigger: 'change'}
                ],
				traceKind: [
					{required: true, message: '请选择跟踪类型', trigger: 'change'}
				],
				traceMan: [
					{required: true, message: '请选择提出人', trigger: 'change'}
				],
				cusProvince: [
					{required: true, message: '请选择省份', trigger: 'change'}
				],
				cusCity: [
					{required: true, message: '请选择城市', trigger: 'change'}
				],
				cusAddress: [
					{required: true, message: '请选择区域', trigger: 'change'}
				],
				traceTime: [
					{required: true, message: '请选择当前跟踪人', trigger: 'change'}
				],
				traceDesc: [
					{required: true, message: '客户描述不能为空', trigger: 'blur'}
				],
				cusFeedback: [
					{required: true, message: '产品意向不能为空', trigger: 'blur'}
				],
				relConnectTel: [
					{required: true, message: '联系方式不能为空', trigger: 'blur'}
				],
				busiOppoType: [
					{required: true, message: '请选择跟踪类型', trigger: 'change'},
				],
			}
		}
	},
	methods: {
		init (id) {
			this.dataForm.id = id || 0
			this.visible = true
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields()
				if (this.dataForm.id) {
					this.$http({
						url: this.$http.adornUrl('/crm/oppo/trace/info/' + this.dataForm.id),
						method: 'get',
						params: this.$http.adornParams()
					}).then(({data}) => {
						this.dataForm = data
					})
				}
				this.listAreaByParentId().then(({data}) => {
					this.provinceList = data
				})
				this.$http({
					url: this.$http.adornUrl('/crm/oppo/page'),
					method: 'get',
					params: this.$http.adornParams(
						Object.assign(
							{
								current: 1,
								size: 100
							},
						)
					)
				}).then(({data}) => {
					console.log('🚀 ~ data', data)
					this.sjList = data.obj.records
				})
				this.$http({
					url: this.$http.adornUrl('/crm/base/emp/page'),
					method: 'get',
					params: this.$http.adornParams(Object.assign({
						current: 1,
						size: 1000
					}))
				}).then(({data}) => {
					this.personList = data.obj.records

				})
			})
		},
		listAreaByParentId (pid) {
			if (!pid) pid = 0
			return this.$http({
				url: this.$http.adornUrl(`/admin/area/listByPid`),
				method: 'get',
				params: this.$http.adornParams({pid})
			})
		},
		// 选择省
		selectProvince (val) {
			this.dataForm.cusCity = null
			this.dataForm.city = ''
			// 获取城市的select
			this.listAreaByParentId(val).then(({data}) => {
				this.cityList = data
			})
		},
		// 选择市
		selectCity (val) {
			this.dataForm.areaId = null
			this.dataForm.area = ''
			// 获取区的select
			this.listAreaByParentId(val).then(({data}) => {
				this.areaList = data
			})
		},
		// 表单提交
		dataFormSubmit: Debounce(function () {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
					let person = this.personList.find((item) => {
						return item.id === this.dataForm.traceMan
					})
					this.$http({
						url: this.$http.adornUrl('/crm/oppo/trace/saveBusiOppoTrace'),
						method: this.dataForm.id ? 'put' : 'post',
						data: this.$http.adornData({
							...this.dataForm,
                            id: this.dataForm.id ? this.dataForm.id : wfy.uuid(32),
							traceStauts: '1',
							busiOppoId: this.dataForm.busiOppoId,
							biz1: person.empName,
						})
					}).then(({data}) => {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.visible = false
								this.$emit('refreshDataList')
							}
						})
					})
				}
			})
		})
	}
}
</script>
